package test;

import com.cn.zaprk.utils.DruidUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;

/**
 * @PackageName: test
 * @ClassName: BatchInsertUser
 * @Description:
 * @author: RZS
 * @date: 2022/4/22  10:01
 */
public class BatchInsertUser {
    public static void main(String[] args) {
        // 批量添加数据

        // 准备连接信息
        Connection conn = null;
        Statement stat = null;

        try {
            // 获取数据库连接对象
            conn = DruidUtils.getConn();
            // 创建预sql对象
            stat = conn.createStatement();
            for (int i = 1; i <= 10000; i++) {
                // 获取系统时间戳
                String time = System.currentTimeMillis() + "";
                // 截取前11位
                String t = time.substring(0, 11);
                t = Long.parseLong(t) + i + "";
                // 创建sql语句
                String sql = "insert into user(username, email, phone, salary) " +
                        "values('张三"+i+"', 'zs"+i+"@163.com', "+t+", "+(i+10.0)+")";
                // 批量添加sql
                stat.addBatch(sql);
            }
            // 执行sql
            stat.executeBatch();
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            DruidUtils.close(null, stat, conn);
        }


    }
}
